using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Comm;

public partial class Web_Admin_Feelings_Feelings_Add : AdminPageBase
{
    private int id = 0;
    private BLL.Feelings bll = new BLL.Feelings();
    private BLL.Classification classbll = new BLL.Classification();
    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.QueryInt("id");
        But_AsSave.Visible = base.CheckPopedomInfo(1013, 2);
        if (!IsPostBack)
        {
            loadType();
            loadData();
        }
    }

    /// <summary>
    /// 加载类型
    /// </summary>
    private void loadType()
    {
        chk_Classification.DataSource = classbll.GetList(0, null, "Ordinal asc");
        chk_Classification.DataBind();
    }

    /// <summary>
    /// 加载数据
    /// </summary>
    private void loadData()
    {
        if (id > 0)
        {
            Model.Feelings model = bll.GetModelBystrWhere("ID="+id);
            if (model == null)
            {
                MessageBox.Show("该对象不存在");
            }

            But_Save.Visible = base.CheckPopedomInfo(1013, 4);
            this.Title = navigation1.Title = lit_title.Text = "感情编辑";
            if (!string.IsNullOrEmpty(model.ClassID))
            {
                foreach (ListItem item in chk_Classification.Items)
                {
                    item.Selected = model.ClassID.Contains("," + item.Value + ",");
                }
            }
            txt_UserID.Text = model.UserID.ToString();
            txt_Title.Text = model.Title;
            txt_Tag.Text = model.Tag;
            txt_Content.Text = model.Content;
            txt_Hits.Text = model.Hits.ToString();
            chk_IsPass.Checked = model.IsPass.Value;
            txt_LoveNum.Text = model.LoveNum.ToString();
            txt_CommentNum.Text = model.CommentNum.ToString();
            txt_AddTime.Text = model.AddTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }
        else
        {
            But_Save.Visible = base.CheckPopedomInfo(1013, 2);
            txt_AddTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void But_Save_Click(object sender, EventArgs e)
    {
        save();
    }

    /// <summary>
    /// 另存为
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void But_AsSave_Click(object sender, EventArgs e)
    {
        id = 0;
        save();
    }

    /// <summary>
    /// 保存
    /// </summary>
    private void save()
    {
        Model.Feelings model = myValidate();
        if (model == null)
        {
            return;
        }

        if (id > 0)
        {
            if (bll.Update(model,"ID="+id) < 1)
            {
                MessageBox.Show("修改失败");
            }
            base.AddLog(2, "感情管理ID:" + id);
        }
        else
        {
            int i = bll.Add(model);
            if (i < 1)
            {
                MessageBox.Show("添加失败");
            }
            base.AddLog(1, "感情管理ID:" + i);
        }

        Response.Redirect("Feelings_List.aspx");
    }

    /// <summary>
    /// 验证
    /// </summary>
    /// <returns></returns>
    private Model.Feelings myValidate()
    {
        string ClassID = "";
        foreach (ListItem item in chk_Classification.Items)
        {
            ClassID += item.Selected ? item.Value + "," : "";
        }
        if (!string.IsNullOrEmpty(ClassID))
        {
            ClassID = "," + ClassID;
        }
        string UserID = txt_UserID.Text.Trim();
        string Title = txt_Title.Text.Trim();
        string Tag = txt_Tag.Text.Trim();
        string Content = txt_Content.Text.Trim();
        string Hits = txt_Hits.Text.Trim();
        bool IsPass = chk_IsPass.Checked;
        string LoveNum = txt_LoveNum.Text.Trim();
        string CommentNum = txt_CommentNum.Text.Trim();
        string AddTime = txt_AddTime.Text.Trim();

        Model.Feelings model = new Model.Feelings();
        model.ClassID = ClassID;
        model.UserID = int.Parse(UserID);
        model.Title = Title;
        model.Tag = Tag;
        model.Content = Content;
        model.Hits = int.Parse(Hits);
        model.IsPass = IsPass;
        model.LoveNum = int.Parse(LoveNum);
        model.CommentNum = int.Parse(CommentNum);
        model.AddTime = DateTime.Parse(AddTime);
        
        return model;
    }
}
